package pl.edu.agh.mwo.ui.chart;

import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

import javax.swing.JFileChooser;

public class ChartsFactory {

	public void saveChartToFile(ChartPanel chartPanel) {
		JFileChooser fc = new JFileChooser();
		if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();

			try {
				org.jfree.chart.ChartUtilities.saveChartAsJPEG(file,
						chartPanel.getChart(), chartPanel.getWidth(),
						chartPanel.getHeight());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
/*
	public static JFreeChart createStackedBarChart() {

		// create the chart...
		final JFreeChart chart = ChartFactory.createStackedBarChart3D(
				"Bar Chart Demo" + "\n" + "fds", // chart title
				"Dni tygodnia", // domain axis label
				"Godziny", // range axis label
				createDatasetforStackedBarChart(), // data
				PlotOrientation.VERTICAL, // orientation
				true, // include legend
				true, false);

		return chart;
	}
*/
	
	public static JFreeChart createPieChart() {
		JFreeChart chart = ChartFactory.createPieChart3D(
				"Which operating system are you using?",
				createDatasetforPieChart(), true, true, false);

		PiePlot3D plot = (PiePlot3D) chart.getPlot();
		plot.setStartAngle(290);
		plot.setDirection(Rotation.CLOCKWISE);
		plot.setForegroundAlpha(0.5f);
		return chart;
	}

	
	private static PieDataset createDatasetforPieChart() {
		DefaultPieDataset result = new DefaultPieDataset();
		result.setValue("Linux", 29);
		result.setValue("Mac", 20);
		result.setValue("Windows", 51);
		return result;
	}


	public static JFreeChart createBarChart() {
		 // create the chart...
       final JFreeChart chart = ChartFactory.createBarChart(
           "Bar Chart Demo",         // chart title
           "Category",               // domain axis label
           "Value",                  // range axis label
           createDatasetforBarChart(), // data
           PlotOrientation.VERTICAL, // orientation
           true,                     // include legend
           true,                     
           false                     
       );

       return chart;	
	
	}
	
	 /**
    * Returns a sample dataset.
    * 
    * @return The dataset.
    */
   private static CategoryDataset createDatasetforBarChart() {
       
       // row keys...
       final String series1 = "First";
       final String series2 = "Second";
       final String series3 = "Third";

       // column keys...
       final String category1 = "Category 1";
       final String category2 = "Category 2";
       final String category3 = "Category 3";
       final String category4 = "Category 4";
       final String category5 = "Category 5";

       // create the dataset...
       final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

       dataset.addValue(1.0, series1, category1);
       dataset.addValue(4.0, series1, category2);
       dataset.addValue(3.0, series1, category3);
       dataset.addValue(5.0, series1, category4);
       dataset.addValue(5.0, series1, category5);

       dataset.addValue(5.0, series2, category1);
       dataset.addValue(7.0, series2, category2);
       dataset.addValue(6.0, series2, category3);
       dataset.addValue(8.0, series2, category4);
       dataset.addValue(4.0, series2, category5);

       dataset.addValue(4.0, series3, category1);
       dataset.addValue(3.0, series3, category2);
       dataset.addValue(2.0, series3, category3);
       dataset.addValue(3.0, series3, category4);
       dataset.addValue(6.0, series3, category5);
       
       return dataset;
       
   }
   	
	
	
}
